/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.create;

import br.com.foxline.enums.CivilStatus;
import br.com.foxline.enums.Gender;
import br.com.foxline.model.Address;
import br.com.foxline.model.City;
import br.com.foxline.model.Construction;
import br.com.foxline.model.Customer;
import br.com.foxline.service.CityService;
import br.com.foxline.service.ConstructionService;
import br.com.foxline.service.CustomerService;
import br.com.foxline.util.Caracter;
import br.com.foxline.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerCreateConstruction implements Serializable {
    @EJB
    private ConstructionService constructionService;
    @EJB
    private CityService cityService;
    @EJB
    private CustomerService customerService;
  
    private Construction construction;
    private Address address;
    private City city;
    private Customer customer;
    private Address addresCustomer;
    private List<Address> addressesCustomer;
    private List<Customer> customers;
    private Customer conjuge;
    private boolean conjugeVisible;
    private List<CivilStatus> civilStatus = new ArrayList<CivilStatus>();
    private List<Gender> genders = new ArrayList<Gender>();

    @PostConstruct
    public void init() {
        this.construction = new Construction();
        this.address = new Address();
        this.city = new City();
        this.customer = new Customer();
        this.addresCustomer = new Address();
        this.customers = new ArrayList<Customer>();
        this.addressesCustomer = new ArrayList<Address>();
        this.conjuge = new Customer();
        this.conjugeVisible = false;
        this.civilStatus = Arrays.asList(CivilStatus.values());
        this.genders = Arrays.asList(Gender.values());
    }

    public void save() {
        this.address.setCity(this.city);
        this.construction.setAddress(address);
        this.construction.setCustumers(customers);
        try {
            constructionService.save(construction);
            Msg.messagemInfo(Msg.SuccessFull);
            init();
        } catch (Exception e) {
            Msg.messagemError(Msg.Failure);
        }
    }

    public void addCustomer() {
        customer = customerService.find(customer.getId());
        if (!customers.contains(customer)) {
            this.customers.add(customer);
        }
        customer = new Customer();
    }

    public void removeCustomer(Customer c) {
        this.customers.remove(c);
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Construction getConstruction() {
        return construction;
    }

    public void setConstruction(Construction construction) {
        this.construction = construction;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public Address getAddresCustomer() {
        return addresCustomer;
    }

    public void setAddresCustomer(Address addresCustomer) {
        this.addresCustomer = addresCustomer;
    }

    public List<Address> getAddressesCustomer() {
        return addressesCustomer;
    }

    public void setAddressesCustomer(List<Address> addresCorrectors) {
        this.addressesCustomer = addresCorrectors;
    }

    public Customer getConjuge() {
        return conjuge;
    }

    public void setConjuge(Customer conjuge) {
        this.conjuge = conjuge;
    }

    public boolean isConjugeVisible() {
        return conjugeVisible;
    }

    public void setConjugeVisible(boolean conjugeVisible) {
        this.conjugeVisible = conjugeVisible;
    }

    public List<CivilStatus> getCivilStatus() {
        return civilStatus;
    }

    public void setCivilStatus(List<CivilStatus> civilStatus) {
        this.civilStatus = civilStatus;
    }

    public List<Gender> getGenders() {
        return genders;
    }

    public void setGenders(List<Gender> genders) {
        this.genders = genders;
    }
    
    public void customerExist() {
        conjuge.setCpf(Caracter.removecaracter(conjuge.getCpf()));
        Customer c = customerService.existCustomer(conjuge);
        if (c != null) {
            conjuge = customerService.find(c.getId());
        }
        if (c == null) {
            String cpf = conjuge.getCpf();
            conjuge = new Customer();
            conjuge.setCpf(cpf);
        }
    }

    public void cleanAddress() {
        this.addresCustomer = new Address();
    }

    public void saveCustomer() {
        if (conjuge.getCpf() != null && conjugeVisible) {
            conjuge.setCpf(Caracter.removecaracter(conjuge.getCpf()));
            customer.setSpouse(conjuge);
        }
        if (!addressesCustomer.isEmpty()) {
            customer.setAddresses(addressesCustomer);
        }
        try {
            customerService.saveCustomer(customer);
            this.customer = customerService.findCustomer(customer).get(0);
            this.customers.add(customer);
            reinitDialogFields();
            RequestContext.getCurrentInstance().execute("dlg.hide()");

        } catch (Exception e) {
            Msg.messagemError(Msg.Failure);
            e.printStackTrace();
        }
    }
    
    public void reinitDialogFields(){
        this.customer = new Customer();
        this.conjuge = new Customer();
        this.addresCustomer = new Address();
        this.addressesCustomer = new ArrayList<Address>();
    }

    public void addAddress() {
        addresCustomer.setCep(Caracter.removecaracter(addresCustomer.getCep()));
        addressesCustomer.add(addresCustomer);
        addresCustomer = new Address();
    }

    public void edit(Address ad, int id) {
        addresCustomer = ad;
        if (addresCustomer.getCity() != null) {
            addresCustomer.setCity(cityService.find(addresCustomer.getCity().getId()));
        }
        addressesCustomer.remove(id);
    }

    public void remove(int id) {
        addressesCustomer.remove(id);
    }
    
    public void handleStatusVisible(){
        conjugeVisible = customer.getCivilStatus().equals(CivilStatus.CASADO);
    }
    
    public void closeDialog(){
        reinitDialogFields();
        RequestContext.getCurrentInstance().execute("dlg.hide()");
    }
}
